/*
 * @Author: Guojian Wang 1085844536@qq.com
 * @Date: 2022-12-17 02:16:14
 * @LastEditTime: 2022-12-17 20:47:03
 * @LastEditors: Guojian Wang 1085844536@qq.com
 * @FilePath: \CloundMusic\pages\search\search.js
 * @Description:
 */
import request from '../../utils/request';

let isSend = false;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // placeholder 的内容
        placeholderContent: '',
        // 热搜榜数据
        hotList: [],
        // 用户输入的表单项数据
        searchContent: '',
        // 关键字模糊匹配的数据
        searchList: [],
        // 搜索历史记录
        historyList: [],
    },

    onLoad(options) {
        // 获取初始化数据
        this.getInitData();

        // 获取历史记录
        this.getSearchHistory();
    },

    async getInitData() {
        let placeholderData = await request('/search/default');
        let hotListData = await request('/search/hot/detail');
        this.setData({
            placeholderContent: placeholderData.data.showKeyword,
            hotList: hotListData.data,
        });
    },

    /**
     * 获取本地历史记录的功能函数
     */
    getSearchHistory() {
        let historyList = wx.getStorageSync('searchHistory');
        if (historyList) {
            this.setData({
                historyList,
            });
        }
    },

    /**
     * 表单项内容发生变化的回调
     */
    handleInputChange(event) {
        // 更新 searchContent 的内容
        this.setData({
            searchContent: event.detail.value.trim(),
        });
        if (isSend) {
            return;
        }

        isSend = true;
        this.getSearchList();
        // 函数节流
        setTimeout(() => {
            isSend = false;
        }, 300);
    },

    async getSearchList() {
        if (!this.data.searchContent) {
            this.setData({
                searchList: [],
            });
            return;
        }
        let { searchContent, historyList } = this.data;
        // 发请求获取关键字模糊匹配数据
        let searchListData = await request('/search', {
            keywords: this.data.searchContent,
            limit: 10,
        });
        this.setData({
            searchList: searchListData.result.songs,
        });

        // 将搜索到的关键字添加到历史记录中
        if (historyList.indexOf(searchContent) !== -1) {
            historyList.splice(historyList.indexOf(searchContent), 1);
        }
        historyList.unshift(searchContent);
        this.setData({
            historyList,
        });

        wx.setStorageSync('searchHistory', historyList);
    },

    /**
     * 清空搜索内容
     */
    clearSearchContent() {
        this.setData({
            searchContent: '',
            searchList: [],
        });
    },

    /**
     * 删除搜索历史记录
     */
    deleteSearchHistory() {
        wx.showModal({
            content: '确认删除吗?',
            success: (res) => {
                if (res.confirm) {
                    // 清空 data 中 historyList
                    this.setData({
                        historyList: [],
                    });
                    // 移除本地的历史记录缓存
                    wx.removeStorageSync('searchHistory');
                }
            },
        });
    },

    onReady() {},

    onShow() {},

    onHide() {},

    onUnload() {},

    onShareAppMessage() {},
});
